////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// CppTestWrapper.cpp
// MathLib
//
// Created by Gregory Maksyuk on 10/23/10.
// Copyright 2010 Immersion Digital. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Includes

#include "CppTestWrapper.h"


using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Implementation

namespace Test
{
	CppTestWrapper::CppTestWrapper ()
	{
		testSuite = new Suite();
		suiteOutput = NULL;
	}

	CppTestWrapper::~CppTestWrapper ()
	{
		if (testSuite != NULL)
			delete testSuite;
		if (suiteOutput != NULL)
			delete suiteOutput;
	}

	void CppTestWrapper::AddTestSuite (auto_ptr<Test::Suite> aTestSuite)
	{
		if (testSuite != NULL)
		{
			testSuite->add(aTestSuite);
		}
	}

	void CppTestWrapper::ConfigureOutputByCommandline (int argc, char *argv[])
	{
		if (testSuite != NULL)
		{
			suiteOutput = GetOutputFromCommandline(argc, argv);
		}
	}

	void CppTestWrapper::RunTests ()
	{
		if (testSuite != NULL && suiteOutput != NULL)
		{
			testSuite->run(*suiteOutput, true);
		}
	}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private methods

	Test::Output* CppTestWrapper::GetOutputFromCommandline (int argc, char* argv[])
	{
		if (argc > 2)
			PrintUsageInfoToCout(); // will not return
		
		Test::Output* output = 0;
		
		if (argc == 1)
			output = new Test::TextOutput(Test::TextOutput::Verbose);
		else
		{
			const char* arg = argv[1];
			if (strcmp(arg, "--compiler") == 0)
				output = new Test::CompilerOutput;
			else if (strcmp(arg, "--html") == 0)
				output =  new Test::HtmlOutput;
			else if (strcmp(arg, "--text-terse") == 0)
				output = new Test::TextOutput(Test::TextOutput::Terse);
			else if (strcmp(arg, "--text-verbose") == 0)
				output = new Test::TextOutput(Test::TextOutput::Verbose);
			else
			{
				cout << "invalid commandline argument: " << arg << endl;
				PrintUsageInfoToCout(); // will not return
			}
		}
		
		return output;
	}

	void CppTestWrapper::PrintUsageInfoToCout ()
	{
		cout << "usage: mytest [MODE]\n"
		<< "where MODE may be one of:\n"
		<< "  --compiler\n"
		<< "  --html\n"
		<< "  --text-terse (default)\n"
		<< "  --text-verbose\n";
		exit(0);
	}
}










